/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.biz.rest;

import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.biz.domain.Project;
import me.zhengjie.modules.biz.service.ProjectService;
import me.zhengjie.modules.biz.service.dto.BindHazardous;
import me.zhengjie.modules.biz.service.dto.ProjectQueryCriteria;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Admin
 * @website https://eladmin.vip
 * @date 2023-03-23
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "project管理")
@RequestMapping("/api/project")
public class ProjectController {

    private final ProjectService projectService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('project:list')")
    public void exportProject(HttpServletResponse response, ProjectQueryCriteria criteria) throws IOException {
        projectService.download(projectService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询project")
    @ApiOperation("" +
            "")
//    @PreAuthorize("@el.check('project:list')")
    public ResponseEntity<Object> queryProject(ProjectQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(projectService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @PostMapping
    @Log("新增project")
    @ApiOperation("新增project")
//    @PreAuthorize("@el.check('project:add')")
    public ResponseEntity<Object> createProject(@Validated @RequestBody Project resources) {
        Long id = SecurityUtils.getCurrentUserId();
        resources.setCreatorId(Math.toIntExact(id));
        return new ResponseEntity<>(projectService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改project")
    @ApiOperation("修改project")
//    @PreAuthorize("@el.check('project:edit')")
    public ResponseEntity<Object> updateProject(@Validated @RequestBody Project resources) {
        projectService.update(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/bind-hazardous")
    @Log("project绑定试剂")
    @ApiOperation("project绑定试剂")
    @AnonymousAccess
//    @PreAuthorize("@el.check('project:edit')")
    public ResponseEntity<Object> bindHazardous(@RequestBody BindHazardous bindHazardous) {
        projectService.bindHazardous(bindHazardous.getId(), bindHazardous.getTotalIds());

        return new ResponseEntity<>(bindHazardous, HttpStatus.OK);
    }

    @DeleteMapping
    @Log("删除project")
    @ApiOperation("删除project")
//    @PreAuthorize("@el.check('project:del')")
    public ResponseEntity<Object> deleteProject(@RequestBody Integer[] ids) {
        projectService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/userProjectList")
    @ApiOperation("个人项目列表")
    @AnonymousAccess
    public ResponseEntity<Object> userProjectList() {
        Long id = SecurityUtils.getCurrentUserId();
        return new ResponseEntity<>(projectService.findUserProjectList(id), HttpStatus.OK);
    }

    @GetMapping("/queryProjectData")
    @ApiOperation("查询项目数据")
    @AnonymousAccess
    public ResponseEntity<Object> queryProjectData(HttpServletRequest request) {
        return new ResponseEntity<>(projectService.queryProjectData(request), HttpStatus.OK);
    }
}
